Java Database Programming with JDBC Java Database Programming with JDBC
by Pratik Patel
Coriolis, The Coriolis Group
ISBN: 1576100561   Pub Date: 10/01/96
  

Previous Table of Contents Next


Chapter 3
Using JDBC Drivers

As a developer who’s using the JDBC, one of the first things you need to understand is how to use JDBC drivers and the JDBC API to connect to a data source. This chapter outlines the steps necessary for you to begin that process. We’ll be covering the details of getting JDBC drivers to work, as well as the driver registration process we touched on in Chapter 1. We’ll also take some time to explore JavaSoft’s JDBC-ODBC Bridge, which allows your Java programs to use ODBC drivers to call ODBC data sources.

Before our discussion gets underway though, I need to point out a few things about JDBC drivers. First, there are no drivers packaged with the JDBC API; you must get them yourself from software vendors. Check out this book’s Web site for links to demo versions of drivers for your favorite database server, as well as free JDBC drivers available on the Internet. Second, if you want to use ODBC, don’t forget that you’ll need ODBC drivers, as well. If you don’t have a database server, but you want to use JDBC, don’t despair: You can use the ODBC drivers packaged with Microsoft Access. Using the JDBC-ODBC Bridge, you can write Java applications that can interact with an Access database.

Unfortunately, applets enforce a security restriction that does not allow access to the local disk, so ODBC drivers might not work in the applet context (inside a Web browser). A future release of the Java Development Kit (JDK) may change or relax this security restriction. A workaround for Java-enabled Web browsers is being prepared, and by the time you read this, it may very well be possible to use the JDBC-ODBC bridge. Using ODBC drivers in Java programs also requires pre-installation of the ODBC drivers and JDBC-ODBC Bridge on the client machine. In contrast, JDBC drivers that are 100 percent Java class files can be downloaded dynamically over the network, along with the calling applet’s class file. I’ll provide a more thorough discussion of this point in Chapter 9.

Quick Start Guide

So you’re a regular Java hacker, and you’ve already figured out how to install the JDBC API package. Now you want to jump right into it. This section will outline the four basic steps for running your first query and getting the results. The steps are explained in greater detail in Chapter 4. Figure 3.1 is a diagram relating the four classes that you’ll call on in your JDBC Java program, and it is the skeleton around which you can build database-aware Java programs. The diagram does not list all of the methods available in the respective classes. See Chapter 12, the JDBC API reference, for the complete class and method list.


Figure 3.1  The JDBC classes to call.

The following (Listing 3.1) is a very simple JDBC application that follows these four steps. It runs a query and gets one row from the returned result. If you don’t understand everything going on here, don’t worry—it’s all explained in detail in Chapter 4.

Listing 3.1 Example JDBC application.

import  java.net.URL;
import  java.sql.*;

class Select {
    public static void main(String argv[]) {
     try {
          new imaginary.sql.iMsqlDriver();
          String url = "jdbc:msql://elanor.oit.unc.edu:1112/bcancer";
         Connection con = DriverManager.getConnection(url, "prpatel", "");
          Statement stmt = con.createStatement();
         ResultSet rs = stmt.executeQuery("SELECT * FROM Users");
            System.out.println("Got results:");
          while(rs.next()) {

                                   String UID=  rs.getString(1);
                                   String Password=  rs.getString(2);
                                   String Last=  rs.getString(3);
                                   String First= rs.getString(4);
                                   String OfficeID= rs.getString(5);

                                   System.out.print(UID +" "+ Password+"
                                   "+Last+" "+First+" "+OfficeID );
                                   System.out.print("\n");

         }
          stmt.close();
          con.close();
      }
       catch( Exception e ) {
           e.printStackTrace();
      }
   }
}

Installing java.sql.*

The java.sql.* package contains the JDBC base API classes, which are supposed to be in the normal java.* hierachy that is distributed as part of the Java API (which includes the java.awt, java.io, and java.lang packages). Currently, the JDBC API is not distributed with the JDK, but it is slated to be included in the next release. I have a sneaking suspicion that the java.sql.* package will also be included in the future APIs of popular Java-enabled Web browsers.


Previous Table of Contents Next